package utility.webmailclient.app.usermanagement.gwt.registration.server;

import java.util.ArrayList;
import java.util.List;

import utility.webmailclient.app.persistance.model.Question;
import utility.webmailclient.app.usermanagement.gwt.registration.rpc.Action;
import utility.webmailclient.app.usermanagement.gwt.registration.rpc.RPCService;
import utility.webmailclient.app.usermanagement.gwt.registration.rpc.Response;
import utility.webmailclient.app.usermanagement.gwt.registration.rpc.command.RetrieveQuestionList;
import utility.webmailclient.app.usermanagement.gwt.registration.rpc.command.RetrieveQuestionListResponse;
import utility.webmailclient.app.usermanagement.gwt.registration.server.dto.QuestionDTO;
import utility.webmailclient.app.usermanagement.persistance.dao.QuestionDAO;

public class QuestionRetrieveServiceImpl implements RPCService {

	private QuestionDAO questionDAO;
	private QuestionDTO questionDTO;
	
	public QuestionDAO getQuestionDAO() {
		return questionDAO;
	}

	public void setQuestionDAO(QuestionDAO questionDAO) {
		this.questionDAO = questionDAO;
	}

	public QuestionDTO getQuestionDTO() {
		return questionDTO;
	}

	public void setQuestionDTO(QuestionDTO questionDTO) {
		this.questionDTO = questionDTO;
	}

	@Override
	public <T extends Response> T execute(Action<T> action) {
		//check for the action
		Response genericResponse = null;
		if(action instanceof RetrieveQuestionList)
		{
			List<Question> dbQuestionList = this.questionDAO.retrieveQuestionList();
			
			RetrieveQuestionListResponse response = new RetrieveQuestionListResponse();
			List<utility.webmailclient.app.usermanagement.gwt.registration.rpc.model.Question> gwtQuestionList = this.questionDTO.toGWTQuestion(dbQuestionList);
			if(gwtQuestionList instanceof ArrayList<?>)
			response.setQuestionList((ArrayList<utility.webmailclient.app.usermanagement.gwt.registration.rpc.model.Question>)gwtQuestionList);
			
			//TODO remove the waring of casting
			return (T) response ;
		}
		return null;
		
	}

}
